﻿//百度服务
function BaiduMapService(layerInfo) {
    var BaiduMapService = new Object;
    BaiduMapService.id = layerInfo.hasOwnProperty("GUID") ? layerInfo.GUID : "";
    BaiduMapService.url = layerInfo.hasOwnProperty("URL") ? layerInfo.URL : "";
    BaiduMapService.name = layerInfo.hasOwnProperty("NAME") ? layerInfo.NAME : "";
    BaiduMapService.servertype = layerInfo.hasOwnProperty("SERVERTYPE") ? layerInfo.SERVERTYPE : "";
    BaiduMapService.prj = layerInfo.hasOwnProperty("PRJ") ? layerInfo.PRJ : "EPSG:3857";
    BaiduMapService.tileGrid = layerInfo.hasOwnProperty("TILEGRID") ? layerInfo.TILEGRID : "EPSG:3857";
    BaiduMapService.CreateLayer = function () {
        var id = this.id;
        var url = this.url;
        var name = this.name;
        var prj = this.prj;
        var servertype = this.servertype;
        var tileGrid = this.tileGrid;
        var baiduSource = new ol.source.TileImage({
            projection: prj,
            tileGrid: tileGrid,
            tileUrlFunction: function (tileCoord, pixelRatio, proj) {
                var z = tileCoord[0];
                var x = tileCoord[1];
                var y = tileCoord[2];
                // 百度瓦片服务url将负数使用M前缀来标识
                if (x < 0) {
                    x = 'M' + (-x);
                }
                if (y < 0) {
                    y = 'M' + (-y);
                }
                return url + "x=" + x + "&y=" + y + "&z=" + z;
            },
            wrapX: false
        });
        baiduSource.set("id", id, true);
        var thisLayer = new ol.layer.Tile({
            source: baiduSource,
            name: name
        });
        return thisLayer;
    }
    function getDefaultTileGrid() {
        //百度地图
        // 自定义分辨率和瓦片坐标系
        var resolutions = [];
        var maxZoom = 18;

        // 计算百度使用的分辨率
        for (var i = 0; i <= maxZoom; i++) {
            resolutions[i] = Math.pow(2, maxZoom - i);
        }
        var tilegrid = new ol.tilegrid.TileGrid({
            origin: [0, 0],    // 设置原点坐标
            resolutions: resolutions    // 设置分辨率
        });
        return tilegrid;
     }
    return BaiduMapService;
}